//
//  SetupViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/27.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "SetupViewController.h"

#import "UserInfoButton.h"
#import "LoginViewController.h"
#import "UserInfoView.h"

@interface SetupViewController ()

@property (nonatomic, strong) UIView * mainView; // 窗口视图
@property (nonatomic, strong) UILabel * titleLabel; // 标题
@property (nonatomic, strong) UserInfoButton * userInfoButton; // 用户信息
@property (nonatomic, strong) UserInfoButton * adviceButton; // 建议
@property (nonatomic, strong) UserInfoButton * clearButton; // 清除缓存
@property (nonatomic, strong) UIButton * quitButton; // 退出

@end

@implementation SetupViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加视图
    [self.view addSubview:self.mainView];
    [_mainView addSubview:self.titleLabel];
    [_mainView addSubview:self.userInfoButton];
    [_mainView addSubview:self.adviceButton];
    [_mainView addSubview:self.clearButton];
    [_mainView addSubview:self.quitButton];
    
    // 自动布局
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.52);
        make.height.equalTo(_mainView.mas_width).multipliedBy(0.6);
        make.centerX.equalTo(self.view).offset(SCREEN_WIDTH * 0.195 * 0.5);
        make.centerY.equalTo(self.view);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_mainView);
        make.centerY.equalTo(_mainView).multipliedBy(0.2);
    }];
    [_userInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_mainView);
        make.height.equalTo(_mainView).multipliedBy(0.2);
        make.centerX.equalTo(_mainView);
        make.centerY.equalTo(_mainView).multipliedBy(0.6);
    }];
    [_adviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_mainView).multipliedBy(0.96);
        make.height.equalTo(_mainView).multipliedBy(0.1);
        make.centerX.equalTo(_mainView);
        make.centerY.equalTo(_mainView);
    }];
    [_clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_adviceButton);
        make.height.equalTo(_mainView).multipliedBy(0.15);
        make.centerX.equalTo(_mainView);
        make.centerY.equalTo(_mainView).multipliedBy(1.37);
    }];
    [_quitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_mainView).multipliedBy(0.7);
        make.height.equalTo(_quitButton.mas_width).multipliedBy(0.11);
        make.centerX.equalTo(_mainView);
        make.centerY.equalTo(_mainView).multipliedBy(1.8);
    }];
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToUserInfoButton {
    UserInfoView * userInfoView = [[UserInfoView alloc] init];
    
    // 添加视图
    [self.view addSubview:userInfoView];
    
    // 自动布局
    [userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.597);
        make.height.equalTo(_userInfoButton.mas_width).multipliedBy(0.68);
        make.centerY.equalTo(self.view);
        make.centerX.equalTo(self.view).offset(SCREEN_WIDTH * 0.195 * 0.5);
    }];
}
- (void)respondsToAdviceButton {
    
}
- (void)respondsToClearButton {
    _clearButton.memorySizeLabel.text = @"0.00M";
}
- (void)respondsToLoginButton {
    // 取到根视图控制器
    UIViewController * rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    // 取到最上面的视图控制器
    UIViewController * topVc = rootVc.childViewControllers.lastObject;
    // 移除最上面的视图和控制器
    [topVc.view removeFromSuperview];
    [topVc removeFromParentViewController];
    // 创建登录视图控制器
    UIStoryboard * mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    LoginViewController * loginVc = [mainSB instantiateViewControllerWithIdentifier:@"LoginViewController"];
    
    // 添加登录视图与控制器
    UIView * superView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [superView addSubview:loginVc.view];
    
    [rootVc addChildViewController:loginVc];
    [rootVc.view insertSubview:superView atIndex:10];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
        _mainView.layer.borderColor = RGB_COLOR(208, 208, 208, 1).CGColor;
        _mainView.layer.borderWidth = 1;
    }
    return _mainView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"设置";
        _titleLabel.textColor = RGB_COLOR(129, 129, 129, 1);
        _titleLabel.font = [UIFont systemFontOfSize:25];
    }
    return _titleLabel;
}
- (UserInfoButton *)userInfoButton {
    if (!_userInfoButton) {
        _userInfoButton = [[UserInfoButton alloc] init];
        [_userInfoButton.headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND([LoginUserInfo defaultUserInfo].head)] placeholderImage:[UIImage imageNamed:@"default_head_s"]];
        [_userInfoButton addTarget:self action:@selector(respondsToUserInfoButton) forControlEvents:UIControlEventTouchUpInside];
        _userInfoButton.layer.borderWidth = 2;
        _userInfoButton.nameLabel.text = [LoginUserInfo defaultUserInfo].username;
    }
    return _userInfoButton;
}
- (UserInfoButton *)adviceButton {
    if (!_adviceButton) {
        _adviceButton = [[UserInfoButton alloc] init];
        _adviceButton.infoLabel.text = @"意见反馈";
        [_adviceButton addTarget:self action:@selector(respondsToAdviceButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _adviceButton;
}
- (UserInfoButton *)clearButton {
    if (!_clearButton) {
        _clearButton = [[UserInfoButton alloc] init];
        _clearButton.infoLabel.text = @"清除离线下载内容";
        _clearButton.memorySizeLabel.text = @"0.27M";
        _clearButton.extensionImageView.hidden = YES;
        [_clearButton addTarget:self action:@selector(respondsToClearButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}
- (UIButton *)quitButton {
    if (!_quitButton) {
        _quitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quitButton setImage:[UIImage imageNamed:@"i_up_退出登陆_03"] forState:UIControlStateNormal];
        _quitButton.adjustsImageWhenHighlighted = NO;
        [_quitButton addTarget:self action:@selector(respondsToLoginButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quitButton;
}

@end
